<?php
namespace app\appadmin\controller\v1;
use app\app\Appbase;

//
//方案管理
//
class Scheme extends Appbase 
{
	
	/**
	* 大客户分析
	* /appadmin/v1/scheme/dkh
	* 是否需要登录 
	* @is_need_login true
	*/
	public function dkh($shop_id=-1){
		
		try {  
			$x = model("admin/system_param_value")
				->where('param_code','buess_shop_khfx_dkh')
				->where('parent_value', $shop_id)
				->value('param_value') ?? '0~100,100~500,500';
				
			$xs =  explode(",",$x);
			
			$data['male']=[];
			$data['female']=[];
			
			foreach($xs as $key => $value){
				$arr = explode("~",$value);
				
				$sex = 1;
				$totalXf =  $this->getModel($sex)->sum('pay_price');
				if($arr[1]){
					$data['male'][] = [
						"title"=>$value."元",
						"num"=>$this->getModel($sex)
							->where('b.pay_price','>=',$arr[0])
							->where('b.pay_price','<=',$arr[1])
							->count(),
						"xf"=> $xf = $this->getModel($sex)
							->where('b.pay_price','>=',$arr[0])
							->where('b.pay_price','<=',$arr[1])
							->sum('pay_price'),
						"zb"=> ($totalXf > 0 ? round($xf / $totalXf * 100,2) : 0) .'%',
					];
				}else{
					$data['male'][] = [
						"title"=>$value."以上",
						"num"=>$this->getModel($sex)->where('b.pay_price','>',$arr[0])->count(),
						"xf"=> $xf = $this->getModel($sex)->where('b.pay_price','>',$arr[0])->sum('pay_price'),
						"zb"=> ($totalXf > 0 ? round($xf / $totalXf * 100,2) : 0) .'%',
					];
				}
				
				$sex = 2;
				$totalXf =  $this->getModel($sex)->sum('pay_price');
				if($arr[1]){
					$data['female'][] = [
						"title"=>$value."元",
						"num"=>$this->getModel($sex)
							->where('b.pay_price','>=',$arr[0])
							->where('b.pay_price','<=',$arr[1])
							->count(),
						"xf"=> $xf = $this->getModel($sex)
							->where('b.pay_price','>=',$arr[0])
							->where('b.pay_price','<=',$arr[1])
							->sum('pay_price'),
						"zb"=> ($totalXf > 0 ? round($xf / $totalXf * 100,2) : 0) .'%',
					];
				}else{
					$data['female'][] = [
						"title"=>$value."以上",
						"num"=>$this->getModel($sex)->where('b.pay_price','>',$arr[0])->count(),
						"xf"=> $xf = $this->getModel($sex)->where('b.pay_price','>',$arr[0])->sum('pay_price'),
						"zb"=> ($totalXf > 0 ? round($xf / $totalXf * 100,2) : 0) .'%',
					];
				}
				
			}
			
			return $this->ReSucess($data);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	//获取模型
	private function getModel($sex=1){
		$shop_id = input('shop_id',-1);
		$model = model('user')->alias('a')
			->leftJoin("( 
				select uid,sum(pay_price) pay_price from xw_order 
				where is_pay = 1 and status >= 0 
				group by uid
			) b",'a.id = b.uid')
			->leftJoin("(
				select uid,add_time,count(*) nums from (
					select * from xw_shop_yy_order  where status = 1 
					order by id desc	
					limit 0,9999999999
				) a group by uid
			) c",'c.uid = a.id')
			->where(function($query) use($sex) {
				$query->where("a.sex",$sex);
				$query->whereor("a.sex", $sex == 1 ? '男' : '女' );
			});	
		if($shop_id!=-1)  $model = $model->where('a.shop_id',$shop_id);
		return $model;
	}
	
	
	/**
	* 忠诚客户分析
	* /appadmin/v1/scheme/zckh
	* 是否需要登录 
	* @is_need_login false
	*/
	public function zckh($shop_id=-1){
		try {  
			$x = model("admin/system_param_value")
				->where('param_code','buess_shop_khfx_zckh')
				->where('parent_value',$shop_id)
				->value('param_value') ?? '0~100,100~500,500';
			
			$xs = explode(",",$x);
			
			$y1 = $this->getModel(1);
			$y2 = $this->getModel(2);
			$totalXf1 =  $y1->sum('pay_price');
			$totalXf2 =  $y2->sum('pay_price');
			
			$data = ['male'=>[],'female'=>[]];
			foreach($xs as $key => $value){
				$arr = explode("~",$value);
				if($arr[1]){
					$data['male'][] = [
						"title" => $value,
						"num" => $y1
							->where('ifnull(c.nums,0) >= '.$arr[0])
							->where('ifnull(c.nums,0) <='.$arr[1])
							->count(),
						"xf" => $xf = $y1
							->where('ifnull(c.nums,0) >= '.$arr[0])
							->where('ifnull(c.nums,0) <='.$arr[1])
							->sum('pay_price'),
						"zb"=> ($totalXf1 > 0 ? round($xf / $totalXf1 * 100,2) : 0) .'%',
					];	
				}else{
					$data['male'][] = [
						"title"=>$value."以上",
						"num"=> $y1->where('ifnull(c.nums,0) > '.$arr[0])->count(),
						"xf"=> $xf = $y1->where('ifnull(c.nums,0) > '.$arr[0])->sum('pay_price'),
						"zb"=> ($totalXf1 > 0 ? round($xf / $totalXf1 * 100,2) : 0) .'%',
					];
				}
				
				if($arr[1]){
					$data['female'][] = [
						"title" => $value,
						"num" => $y2
							->where('ifnull(c.nums,0) >= '.$arr[0])
							->where('ifnull(c.nums,0) <='.$arr[1])
							->count(),
						"xf" => $xf = $y2
							->where('ifnull(c.nums,0) >= '.$arr[0])
							->where('ifnull(c.nums,0) <='.$arr[1])
							->sum('pay_price'),
						"zb"=> ($totalXf2 > 0 ? round($xf / $totalXf2 * 100,2) : 0) .'%',
					];	
				}else{
					$data['female'][] = [
						"title"=>$value."以上",
						"num"=> $y2->where('ifnull(c.nums,0) > '.$arr[0])->count(),
						"xf"=> $xf = $y2->where('ifnull(c.nums,0) > '.$arr[0])->sum('pay_price'),
						"zb"=> ($totalXf2 > 0 ? round($xf / $totalXf2 * 100,2) : 0) .'%',
					];
				}
			}	
			
			return $this->ReSucess($data);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 活跃客户分析
	* /appadmin/v1/scheme/hykh
	* 是否需要登录 
	* @is_need_login true
	*/
	public function hykh(){
		try {  
			
			$x =  '0~100,100~500,500';
			
			$xs = explode(",",$x);
			$y1 = $this->getModel(1);
			$y2 = $this->getModel(2);
			$totalXf1 =  $y1->sum('pay_price');
			$totalXf2 =  $y2->sum('pay_price');
			
			$data = ['male'=>[],'female'=>[]];
			foreach($xs as $key => $value){
				$arr = explode("~",$value);
				if($arr[1]){
					$data['male'][] = [
						"title" => $value,
						"num" => $y1
							->where("-datediff(ifnull(c.add_time,a.add_time),now()) >= ".$arr[0])
							->where("-datediff(ifnull(c.add_time,a.add_time),now()) <= ".$arr[1])
							->count(),
						"xf" => $xf = $y1
							->where("-datediff(ifnull(c.add_time,a.add_time),now()) >= ".$arr[0])
							->where("-datediff(ifnull(c.add_time,a.add_time),now()) <= ".$arr[1])
							->sum('pay_price'),
						"zb"=> ($totalXf1 > 0 ? round($xf / $totalXf1 * 100,2) : 0) .'%',
					];	
				}else{
					$data['male'][] = [
						"title"=>$value."以上",
						"num"=> $y1
							->where('-datediff(ifnull(c.add_time,a.add_time),now()) > '.$arr[0])
							->count(),
						"xf"=> $xf = $y1
							->where('-datediff(ifnull(c.add_time,a.add_time),now()) > '.$arr[0])
							->sum('pay_price'),
						"zb"=> ($totalXf1 > 0 ? round($xf / $totalXf1 * 100,2) : 0) .'%',
					];
				}
				
				
				if($arr[1]){
					$data['female'][] = [
						"title" => $value,
						"num" => $y2
							->where("-datediff(ifnull(c.add_time,a.add_time),now()) >= ".$arr[0])
							->where("-datediff(ifnull(c.add_time,a.add_time),now()) <= ".$arr[1])
							->count(),
						"xf" => $xf = $y2
							->where("-datediff(ifnull(c.add_time,a.add_time),now()) >= ".$arr[0])
							->where("-datediff(ifnull(c.add_time,a.add_time),now()) <= ".$arr[1])
							->sum('pay_price'),
						"zb"=> ($totalXf2 > 0 ? round($xf / $totalXf2 * 100,2) : 0) .'%',
					];	
				}else{
					$data['female'][] = [
						"title"=>$value."以上",
						"num"=> $y2
							->where('-datediff(ifnull(c.add_time,a.add_time),now()) > '.$arr[0])
							->count(),
						"xf"=> $xf = $y2
							->where('-datediff(ifnull(c.add_time,a.add_time),now()) > '.$arr[0])
							->sum('pay_price'),
						"zb"=> ($totalXf2 > 0 ? round($xf / $totalXf2 * 100,2) : 0) .'%',
					];
				}
				
			}	
		
			return $this->ReSucess($data);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 重点客户分析
	* /appadmin/v1/scheme/zdkh
	* 是否需要登录 
	* @is_need_login true
	*/
	public function zdkh($shop_id){
		try {  
			$data['zd'] = [
				[
					"month"=>1,
					"num"=>12,
					"xf"=>0,
					"new"=>0,
				],
				[
					"month"=>1,
					"num"=>12,
					"xf"=>0,
					"new"=>0,
				],
			];
			$data['ls'] = [
				[
					"month"=>1,
					"num"=>12,
					"xf"=>0,
					"new"=>0,
				],
				[
					"month"=>1,
					"num"=>12,
					"xf"=>0,
					"new"=>0,
				],
			];
			
			
			return $this->ReSucess($data);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	
	/**
	* 会员增长分析
	* /appadmin/v1/scheme/hyzz
	* 是否需要登录 
	* @is_need_login true
	*/
	public function hyzz($shop_id,$type=1){
		try {  
			if($type==2){
				$data = [
					[
						"date"=>"1月",
						"num"=>12,
					],
					[
						"date"=>"2月",
						"num"=>12,
					],
					[
						"date"=>"3月",
						"num"=>0,
					],
				];
			} else if($type==3){
				$data = [
					[
						"date"=>"2021年",
						"num"=>12,
					],
				];
			}else {
				$data = [
					[
						"date"=>"1号",
						"num"=>12,
					],
					[
						"date"=>"2号",
						"num"=>12,
					],
				];
			}
			
			return $this->ReSucess($data);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 消费结构分析
	* /appadmin/v1/scheme/xfjg
	* 是否需要登录 
	* @is_need_login true
	*/
	public function xfjg($shop_id,$type=1){
		try {  
			$data = [
				[
					"type"=>"会员男",
					"num"=>12,
				],
				[
					"type"=>"会员女",
					"num"=>0,
				],
				[
					"type"=>"散客男",
					"num"=>0,
				],
				[
					"type"=>"散客女",
					"num"=>0,
				],
			];
			
			return $this->ReSucess($data);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
}



